/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.member.forms;

import javax.servlet.http.HttpServletRequest;
import model.UserService;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author SONY
 */
public class ProfileStrutsActionForm extends org.apache.struts.action.ActionForm {
    
    private String firstName;
    private String lastName;
    private String address;
    private String phone;
    private String gender;
    private String email;
    private String passwordOld;
    private String passwordNew;
    private String confirmpassNew;
    private boolean changePass = false;

    /**
     *
     */
    public ProfileStrutsActionForm() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        
        ActionErrors errors = new ActionErrors();
        
        UserService user_service = new UserService();
        if(changePass) {
            if (passwordOld.length() == 0) {
                errors.add("passwordOld", new ActionMessage("error.member.profile.passwordOldField.required"));
            } else if(user_service.checkPassword(email, passwordOld)) {
                if (passwordNew.length() == 0) {
                    errors.add("passwordNew", new ActionMessage("error.member.profile.passwordNewField.required"));
                } else if(passwordNew.length() < 6 || passwordNew.length() > 20) {
                   errors.add("passwordNew", new ActionMessage("error.member.profile.passwordNewField.range"));
                }
                if (confirmpassNew.length() == 0) {
                    errors.add("confirmpassNew", new ActionMessage("error.member.profile.confirmpassNewField.required"));
                } else if (!confirmpassNew.equals(passwordNew)) {
                    errors.add("confirmpassNew", new ActionMessage("error.member.profile.confirmpassNewField.compare"));
                }
            } else {
                errors.add("passwordOld", new ActionMessage("error.member.profile.passwordOldField.exist"));
            } 
        }
        if (lastName.length() == 0) {
            errors.add("lastName", new ActionMessage("error.nonmember.register.lastNameField.required"));
        }
        if (firstName.length() == 0) {
            errors.add("firstName", new ActionMessage("error.nonmember.register.firstNameField.required"));
        }
        String phoneExpr = "([0-9]+)";
        if (phone.length() == 0) {
            errors.add("phone", new ActionMessage("error.nonmember.register.phoneField.required"));
        } else if(!phone.matches(phoneExpr)) {
           errors.add("phone", new ActionMessage("error.nonmember.register.phoneField.format"));
        }  
        if (address.length() == 0) {
            errors.add("address", new ActionMessage("error.member.profile.addressField.required"));
        }                   
        
        return errors;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }

    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the passwordOld
     */
    public String getPasswordOld() {
        return passwordOld;
    }

    /**
     * @param passwordOld the passwordOld to set
     */
    public void setPasswordOld(String passwordOld) {
        this.passwordOld = passwordOld;
    }

    /**
     * @return the passwordNew
     */
    public String getPasswordNew() {
        return passwordNew;
    }

    /**
     * @param passwordNew the passwordNew to set
     */
    public void setPasswordNew(String passwordNew) {
        this.passwordNew = passwordNew;
    }

    /**
     * @return the confirmpassNew
     */
    public String getConfirmpassNew() {
        return confirmpassNew;
    }

    /**
     * @param confirmpassNew the confirmpassNew to set
     */
    public void setConfirmpassNew(String confirmpassNew) {
        this.confirmpassNew = confirmpassNew;
    }

    /**
     * @return the changePass
     */
    public boolean getChangePass() {
        return changePass;
    }

    /**
     * @param changePass the changePass to set
     */
    public void setChangePass(boolean changePass) {
        this.changePass = changePass;
    }
}
